<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=yes">
  
  
  <title>  Vue-Cli 3.0请求本地模拟数据 |   写夜子的个人博客 </title>

 
  
    <link rel="icon" href="/images/favicon.png">
  


  <link rel="stylesheet" href="/nayo.min.css"> 
</head>  
  <body>   
    
      <header class="header">
	
  <nav class="header-nav">        
   
    <span class="iconfont icon-menu mobile-toggle"></span>   	

    <div class="header-menu">          
              
            
              <a class="header-menu-link" id="header-menu-home" href="/">首页</a>     
            
            
            
              <a class="header-menu-link" id="header-menu-archives" href="/archives">归档</a>     
            
            
            
              <a class="header-menu-link" id="header-menu-tags" href="/tags">标签</a>     
            
            
            
              <a class="header-menu-link" id="header-menu-about" href="/about">关于</a>     
            
            
            
              <a class="iconfont icon-menu-search header-menu-link" id="header-menu-search"></a>
            
                
    </div>  
    
  </nav>
</header>   

      <div class="container">       
          
          
            <section class="main">  
          

          <article class="post">
  
	<div class="post-header">

	<p class="post-title">	
		Vue-cli 3.0请求本地模拟数据
	</p>
			

	<div class="meta-info">	
	<span>
		10月 11, 2018
	</span>

	
	
		<i class="iconfont icon-words"></i>
		<span>
			1209
		</span>
	
</div>

</div> 
	 

	  <div class="post-content slideDownMin">

		

			
					<h3 id="升级的前后"><a href="#升级的前后" class="headerlink" title="升级的前后"></a>升级的前后</h3><p>  很多时候,我们在一个项目的开发阶段,需要用到本地的模拟数据.在3.0以前的版本,在项目的根目录下有一个webpack.dev.conf.js文件可以提供给开发者进行配置,但是现在升级之后,3.0的版本目录结构比以前精简很多,没有了这个文件,那么在这个版本我们怎么请求本地的模拟数据呢？<br>  <a id="more"></a></p>
<h3 id="请求本地模拟数据"><a href="#请求本地模拟数据" class="headerlink" title="请求本地模拟数据"></a>请求本地模拟数据</h3><ol>
<li>将模拟数据源文件放置到根目录下(data.json)   </li>
</ol>
<p>  <img src="http://wx2.sinaimg.cn/mw690/89296167gy1fw423ia7kej20xu0w00z6.jpg" alt="data.json" title="文件位置"></p>
<ol start="2">
<li>在根目录下新建vue.config.js文件,并添加如下代码:    </li>
</ol>
  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// vue.config.js</span></span><br><span class="line">  <span class="keyword">const</span> express = <span class="built_in">require</span>(<span class="string">'express'</span>) <span class="comment">//引入express框架</span></span><br><span class="line">  <span class="keyword">const</span> app = express() <span class="comment">//实例化对象</span></span><br><span class="line">  <span class="keyword">var</span> appData = <span class="built_in">require</span>(<span class="string">'./data.json'</span>) <span class="comment">//引入存放数据的json</span></span><br><span class="line">  <span class="keyword">var</span> seller = appData.seller;<span class="comment">//取出数据</span></span><br><span class="line">  <span class="keyword">var</span> goods = appData.goods;</span><br><span class="line">  <span class="keyword">var</span> ratings = appData.ratings;</span><br><span class="line">  <span class="keyword">var</span> apiRoutes = express.Router(); <span class="comment">//引入路由</span></span><br><span class="line">  app.use(<span class="string">'/api'</span>,apiRoutes)</span><br><span class="line">  <span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    <span class="comment">// 选项...</span></span><br><span class="line">    devServer: &#123;</span><br><span class="line">    <span class="comment">//模拟数据开始</span></span><br><span class="line">        before(app) &#123;</span><br><span class="line">            app.get(<span class="string">'/api/seller'</span>, (req, res) =&gt; &#123;</span><br><span class="line">                res.json(&#123;</span><br><span class="line">                    <span class="comment">// 这里是你的json内容</span></span><br><span class="line">                    errno: <span class="number">0</span>,</span><br><span class="line">                    data: seller</span><br><span class="line">                &#125;)</span><br><span class="line">            &#125;),</span><br><span class="line">            app.get(<span class="string">'/api/goods'</span>, (req, res) =&gt; &#123;</span><br><span class="line">                res.json(&#123;</span><br><span class="line">                    <span class="comment">// 这里是你的json内容</span></span><br><span class="line">                    errno: <span class="number">0</span>,</span><br><span class="line">                    data: goods</span><br><span class="line">                &#125;)</span><br><span class="line">            &#125;),</span><br><span class="line">            app.get(<span class="string">'/api/ratings'</span>, (req, res) =&gt; &#123;</span><br><span class="line">                res.json(&#123;</span><br><span class="line">                    <span class="comment">// 这里是你的json内容</span></span><br><span class="line">                    errno: <span class="number">0</span>,</span><br><span class="line">                    data: ratings</span><br><span class="line">                &#125;)</span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<p>  从上面的代码可以看出,我们便可以通过’/api/seller’这个接口请求到本地的模拟数据</p>
<ol start="3">
<li>取出数据         </li>
</ol>
<p>  在任意一个组件内,利用axios进行请求:<br>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">created()&#123;</span><br><span class="line">  <span class="keyword">this</span>.$axios.get(<span class="string">"/api/seller"</span>)</span><br><span class="line">  .then(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(res.data);</span><br><span class="line">  &#125;)</span><br><span class="line">  .catch(<span class="function"><span class="params">error</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(error);</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>  我们就可以拿到本地的模拟数据了:<br>  <img src="http://wx2.sinaimg.cn/mw690/89296167gy1fw41qkqekwj20uy0uqwlr.jpg" alt="data.json" title="文件位置"></p>
  	
					
	  </div>     
	  

	
<div class="post-meta">
      	

      
        <i class="iconfont icon-tag"></i>     
          <a class="tag-link" href="/tags/JS/">JS</a> <a class="tag-link" href="/tags/Vue/">Vue</a>    
      	
</div>





<div class="post-footer">
  <div class="pf-left">
      <img class="pf-avatar" src="http://wx2.sinaimg.cn/mw690/89296167gy1fu5ph67x17j21kw1kwhdu.jpg">
      <p class="pf-des">hi,我是写夜子,希望你有愉快的一天.</p>
  </div>

  <div class="pf-right">           
      <div class="pf-links">
        




<span class="donate-btn">
	<span class="iconfont icon-donate"></span>
</span>


<div id="donate-box" class="sildeUpMin">

	<span class="donate-cancel iconfont icon-cancel"></span>

	<div class="donate-img-box">
		<img id="donate-qr-wechat" class="noLazyLoad donate-img" src="/images/donate1.png" alt="No Donate Image!">	
		<img id="donate-qr-alipay" class="noLazyLoad donate-img" src="/images/donate2.png" alt="No Donate Image!">	
	</div>

	<span class="donate-word">世界美好 你也是</span>

	<div class="donate-list">
		<span class="iconfont icon-donate-wechat"></span>
		<span class="iconfont icon-donate-alipay"></span>
	</div>

</div>

 
        
	
<script id="-mob-share" src="http://f1.webshare.mob.com/code/mob-share.js?appkey=21d601593a1de"></script>
	
	<span class="share-btn">
	<span class="iconfont icon-share"></span>
	</span>


	<div class="-mob-share sildeUpMin">
		   			             
            <a class="iconfont  icon-share-qq -mob-share-qq"></a>		
     	   			             
            <a class="iconfont  icon-share-weixin -mob-share-weixin"></a>		
     	   			             
            <a class="iconfont  icon-share-weibo -mob-share-weibo"></a>		
     	   			             
            <a class="iconfont  icon-share-douban -mob-share-douban"></a>		
     	   			             
            <a class="iconfont  icon-share-facebook -mob-share-facebook"></a>		
     	   			             
            <a class="iconfont  icon-share-twitter -mob-share-twitter"></a>		
     	   			             
            <a class="iconfont  icon-share-google -mob-share-google"></a>		
     	   
	</div>	

      </div>  
    <nav class="pf-paginator">
      
         
          <a href="/2018/10/24/Es6的学习笔记1/" data-hover="Es6的学习笔记1">上一篇</a>      
            
        
      
        
        <a href="/2018/10/08/Vue-cli 3.0解决跨域问题/" data-hover="Vue-cli 3.0解决跨域问题"> 下一篇</a>
            
  </nav>   
  </div>
</div> 
	
	
</article>

          </section> 
      </div>            
    
    <a id="backTop">
      <span>
        <i class="iconfont icon-backtotop"></i>
      </span>
    </a> 

  
    
    <div class="search-container sildeUpMin">
        <div class="search-header">
            <input type="text" placeholder="输入你想搜索的" id="search-input" class="search-input">  
            <span class="search-cancel iconfont icon-cancel"></span>
        </div>
        <div id="search-result" class="search-result"></div>
    </div>
 
     <div class="mobile-menu">      

      
      <img class="mobile-menu-icon lazyload" src="/images/placeholder.png" data-src="/images/favicon.png">   
      

         
            

            <a class="mobile-menu-link" href="/">首页
            </a>
            
         
            

            <a class="mobile-menu-link" href="/archives">归档
            </a>
            
         
            

            <a class="mobile-menu-link" href="/tags">标签
            </a>
            
         
            

            <a class="mobile-menu-link" href="/about">关于
            </a>
            
         
                          

            <a class="mobile-menu-link mobile-menu-search" href="#">搜索 </a>                 
            
         
      
</div>        
    


<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?636802045446222199ae541e32c8133e"; 
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>





     
    




<footer id="footer">


		<div class="footer-copyright">
		&copy;
		
		2018-
		
		2020

		Xieyezi
		<br>

		Copyright
		<a href="https://github.com/xieyezi" target="_blank">Xieyezi</a>
		</div>

</footer>
   

  

    <script src="/nayo.bundle.js"></script>           
  </body>        
</html>